package com.haixiaoke.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.haixiaoke.common.utils.DateUtils;
import com.haixiaoke.domain.SmartLock;
import com.haixiaoke.domain.SmartLockCard;
import com.haixiaoke.mapper.SmartLockCardMapper;
import com.haixiaoke.service.ISmartLockCardService;
import com.haixiaoke.smartLock.tianwanghulian.SmartLockTWHlService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 智能锁门卡授权信息Service业务层处理
 *
 * @author xiaolong
 * @date 2023-05-31
 */
@Service
public class SmartLockCardServiceImpl implements ISmartLockCardService {
    @Autowired
    private SmartLockCardMapper smartLockCardMapper;
    @Autowired
    private SmartLockTWHlService smartLockTWHlService;

    /**
     * 查询智能锁门卡授权信息
     *
     * @param cardId 智能锁门卡授权信息主键
     * @return 智能锁门卡授权信息
     */
    @Override
    public SmartLockCard selectSmartLockCardByCardId(Long cardId) {
        return smartLockCardMapper.selectSmartLockCardByCardId(cardId);
    }

    /**
     * 查询智能锁门卡授权信息列表
     *
     * @param smartLockCard 智能锁门卡授权信息
     * @return 智能锁门卡授权信息
     */
    @Override
    public List<SmartLockCard> selectSmartLockCardList(SmartLockCard smartLockCard) {
        return smartLockCardMapper.selectSmartLockCardList(smartLockCard);
    }

    /**
     * 新增智能锁门卡授权信息和授权门卡
     *
     * @param smartLockCard 智能锁门卡授权信息
     * @return 结果
     */
    @Override
    public int insertSmartLockCard(SmartLockCard smartLockCard) {

        smartLockCard.setCreateTime(DateUtils.getNowDate());
        return smartLockCardMapper.insertSmartLockCard(smartLockCard);
    }

    /**
     * 修改智能锁门卡授权信息
     *
     * @param smartLockCard 智能锁门卡授权信息
     * @return 结果
     */
    @Override
    public int updateSmartLockCard(SmartLockCard smartLockCard) {
        smartLockCard.setUpdateTime(DateUtils.getNowDate());
        return smartLockCardMapper.updateSmartLockCard(smartLockCard);
    }

    /**
     * 批量删除智能锁门卡授权信息
     *
     * @param cardIds 需要删除的智能锁门卡授权信息主键
     * @return 结果
     */
    @Override
    public int deleteSmartLockCardByCardIds(Long[] cardIds) {
        return smartLockCardMapper.deleteSmartLockCardByCardIds(cardIds);
    }

    /**
     * 删除智能锁门卡授权信息信息
     *
     * @param cardId 智能锁门卡授权信息主键
     * @return 结果
     */
    @Override
    public int deleteSmartLockCardByCardId(Long cardId) {
        return smartLockCardMapper.deleteSmartLockCardByCardId(cardId);
    }

    /**
     * 删除智能锁门卡授权信息和授权门卡授权
     *
     * @param sn             智能锁sn
     * @param identification 门卡标识
     * @return
     */
    @Override
    public int deleteSmartLockCardBySnAndIdentification(String sn, String identification) {
        return smartLockCardMapper.deleteSmartLockCardBySnAndIdentification(sn, identification);
    }

    @Override
    public int deleteSmartLockCardBySnAndIdentificationAndCardType(String sn, String identification, Integer cardType) {
        return smartLockCardMapper.deleteSmartLockCardBySnAndIdentificationAndCardType(sn, identification,cardType);
    }

    @Override
    public List<SmartLockCard> selectSmartLockCardBySN(String sn) {
        SmartLockCard smartLockCard = new SmartLockCard();
        smartLockCard.setSn(sn);
        return smartLockCardMapper.selectSmartLockCardList(smartLockCard);
    }

    @Override
    public int deleteSmartLockCardByReturnCardId(Long returnCardId) {
        return smartLockCardMapper.deleteSmartLockCardByReturnCardId(returnCardId);
    }

    @Override
    public SmartLockCard selectSmartLockPwdBySNAndIdentificationAndCardType(String sn, String identification, Integer cardType) {
        return smartLockCardMapper.selectSmartLockPwdBySNAndIdentificationAndCardType(sn, identification, cardType);
    }

    public int deleteSmartLockCardBySn(String sn) {
        return smartLockCardMapper.deleteSmartLockCardBySn(sn);
    }

    public int deleteSmartLockCardByEndTime(DateTime date) {
        return smartLockCardMapper.deleteSmartLockCardByEndTime(date);
    }
}
